import 'network_utils.dart';
import '../models/order_list_model.dart';
import '../models/account_ye_model.dart';

class CheckSpuConfig {
  static final CheckSpuConfig _instance = CheckSpuConfig._internal();
  
  factory CheckSpuConfig() {
    return _instance;
  }
  
  CheckSpuConfig._internal();

  Future<bool> checkSpuConfigWithId(String supId) async {
    try {
      final response = await NetworkUtils.get(
        RequestCmd.checkSpuConfig,
        params: {'spuId': supId},
      );
      
      final model = CheckSupModel.fromJson(response);
      if (model.code == 200 && model.data == true) {
        return true;
      }
      return false;
    } catch (e) {
      return false;
    }
  }

  Future<Map<String, dynamic>> checkMaxcoinWithId(int supId, int priceNum) async {
    try {
      final response = await NetworkUtils.post(
        RequestCmd.checkout,
        body: {"itemId": supId, "num": priceNum},
      );
      
      final model = ConfirmInfoModel.fromJson(response);
      if (model.code == 200) {
        return {"success": true, "message": model.msg};
      } else {
        return {"success": false, "message": model.msg};
      }

    } catch (e) {
      return {"success": true, "message": ""};
    }
  }
}

class CheckSupModel {
  int code;
  String msg;
  bool data;

  CheckSupModel({
    this.code = 0,
    this.msg = "",
    this.data = false,
  });

  // 从 JSON 转换为 CheckSupModel
  factory CheckSupModel.fromJson(Map<String, dynamic> json) {
    return CheckSupModel(
      code: json['code'] ?? 0,
      msg: json['msg'] ?? '',
      data: json['data'] ?? false,
    );
  }

  // 转换为 JSON
  Map<String, dynamic> toJson() {
    return {
      'code': code,
      'msg': msg,
      'data': data,
    };
  }
}

class ConfirmInfoModel {
  int code;
  String msg;
  ConfirmDetailModel? data;

  ConfirmInfoModel({
    this.code = 0,
    this.msg = "",
    this.data,
  });

  // 从 JSON 转换为 ConfirmInfoModel
  factory ConfirmInfoModel.fromJson(Map<String, dynamic> json) {
    return ConfirmInfoModel(
      code: json['code'] ?? 0,
      msg: json['msg'] ?? '',
      data: json['data'] != null
          ? ConfirmDetailModel.fromJson(json['data'])
          : null,
    );
  }

  // 转换为 JSON
  Map<String, dynamic> toJson() {
    return {
      'code': code,
      'msg': msg,
      'data': data?.toJson(),
    };
  }
}

class ConfirmDetailModel {
  List<GroupListModel> voList;
  ConfirmModel? spuCheckout;
  List<PayListModel> paymentList;

  ConfirmDetailModel({
    this.voList = const [],
    this.spuCheckout,
    this.paymentList = const [],
  });

  // 从 JSON 转换为 ConfirmDetailModel
  factory ConfirmDetailModel.fromJson(Map<String, dynamic> json) {
    var voListFromJson = json['voList'] as List? ?? [];
    List<GroupListModel> voList =
        voListFromJson.map((i) => GroupListModel.fromJson(i)).toList();

    ConfirmModel? spuCheckout;
    if (json['spuCheckout'] != null) {
      spuCheckout = ConfirmModel.fromJson(json['spuCheckout']);
    }

    var paymentListFromJson = json['paymentList'] as List? ?? [];
    List<PayListModel> paymentList =
        paymentListFromJson.map((i) => PayListModel.fromJson(i)).toList();

    return ConfirmDetailModel(
      voList: voList,
      spuCheckout: spuCheckout,
      paymentList: paymentList,
    );
  }

  // 转换为 JSON
  Map<String, dynamic> toJson() {
    return {
      'voList': voList.map((e) => e.toJson()).toList(),
      'spuCheckout': spuCheckout?.toJson(),
      'paymentList': paymentList.map((e) => e.toJson()).toList(),
    };
  }
}

class ConfirmModel {
  String name;
  double balance;
  String subtotalMoney;
  List<TxInfoModel> parameterList;
  String rechargeDetail;
  String rechargeDetailUrl;

  ConfirmModel({
    this.name = '',
    this.balance = 0.0,
    this.subtotalMoney = '',
    this.parameterList = const [],
    this.rechargeDetail = '',
    this.rechargeDetailUrl = '',
  });

  // 从 JSON 转换为 ConfirmModel
  factory ConfirmModel.fromJson(Map<String, dynamic> json) {
    var list = json['parameterList'] as List? ?? [];
    List<TxInfoModel> parameterList =
        list.map((i) => TxInfoModel.fromJson(i)).toList();

    return ConfirmModel(
      name: json['name'] ?? '',
      balance: json['balance']?.toDouble() ?? 0.0,
      subtotalMoney: json['subtotalMoney'] ?? '',
      parameterList: parameterList,
      rechargeDetail: json['rechargeDetail'] ?? '',
      rechargeDetailUrl: json['rechargeDetailUrl'] ?? '',
    );
  }

  // 转换为 JSON
  Map<String, dynamic> toJson() {
    return {
      'name': name,
      'balance': balance,
      'subtotalMoney': subtotalMoney,
      'parameterList': parameterList.map((e) => e.toJson()).toList(),
      'rechargeDetail': rechargeDetail,
      'rechargeDetailUrl': rechargeDetailUrl,
    };
  }
}

class TxInfoModel {
  int type;
  String blurb;
  String name;
  String options;
  String nameZH;
  int required;

  TxInfoModel({
    this.type = 0,
    this.blurb = '',
    this.name = '',
    this.options = '',
    this.nameZH = '',
    this.required = 0,
  });

  // 从 JSON 转换为 TxInfoModel
  factory TxInfoModel.fromJson(Map<String, dynamic> json) {
    return TxInfoModel(
      type: json['type'] ?? 0,
      blurb: json['blurb'] ?? '',
      name: json['name'] ?? '',
      options: json['options'] ?? '',
      nameZH: json['nameZH'] ?? '',
      required: json['required'] ?? 0,  
    );
  }

  // 转换为 JSON
  Map<String, dynamic> toJson() {
    return {
      'type': type,
      'blurb': blurb,
      'name': name,
      'options': options,
      'nameZH': nameZH,
      'required': required,
    };
  }
}
